<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Install</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />                        
  </head>
<body>
<?php
if (isset($_POST['install']))
{ 
    $f = fopen("./includes/conf.inc.php","w");
        
    $s = "<?php\r\n";
    
    $s .= "\$db_host = ".$_POST['db_host'].";\r\n";
    $s .= "\$db_usr = ".$_POST['db_usr'].";\r\n";
    $s .= "\$db_pass = ".$_POST['db_pass'].";\r\n";
    
    $s .= "\$db_name = ".$_POST['db_name'].";\r\n";
    $s .= "\$gal_tbl = ".$_POST['gal_tbl'].";\r\n"; 
    $s .= "\$usr_tbl = ".$_POST['usr_tbl'].";\r\n";
    $s .= "\$discuz_tbl = ".$_POST['discuz_tbl'].";\r\n";
    
    $s .= "\$vyska_alb = ".$_POST['alb_h'].";\r\n";
    $s .= "\$sirka_alb = ".$_POST['alb_w'].";\r\n";
    $s .= "?>";
    
    fwrite($f, $s);
    fclose($f);
    
    include("includes/conf.inc.php");
    
    $id = mysql_connect($db_host,$db_usr,$db_pass) or die('Could not connect: ' . mysql_error()."<br />");

    $query = "CREATE DATABASE IF NOT EXISTS $db_name";
    //echo $query;
    mysql_query($query, $id) or die(mysql_error()."<br />");
    echo "DB CREATED ! <br />";

    mysql_select_db($db_name, $id) or die(mysql_error()."<br />");
    
    $query = "CREATE TABLE IF NOT EXISTS $usr_tbl
              (
              id int UNSIGNED not null AUTO_INCREMENT primary key, 
              username varchar(20), 
              password varchar(32)
              )";
    mysql_query($query, $id) or die(mysql_error()."<br />");
    echo "USER TABLE CREATED ! <br/>";
    
    $query = "CREATE TABLE IF NOT EXISTS $gal_tbl
              (
                id int UNSIGNED not null AUTO_INCREMENT primary key,
                owner int UNSIGNED not null,
                link varchar(32),
                description text,
                foreign key (owner) references $usr_tbl (id) on delete cascade
              )";
               
    mysql_query($query, $id) or die(mysql_error()."<br />");
    echo "GALLERY TABLE CREATED ! <br />";
                 
    $query = "CREATE TABLE IF NOT EXISTS $discuz_tbl
              (
                id int UNSIGNED not null AUTO_INCREMENT primary key,
                id_img int UNSIGNED not null,
                sender varchar(20),
                comment text not null,
                foreign key (id_img) references $gal_tbl (id) on delete cascade
              )";
              
    mysql_query($query, $id) or die(mysql_error()."<br />");
    echo "IMAGE COMMENTS TABLE CREATED ! <br />";
    
    $dfpass = md5("default");
    
    $query = "INSERT INTO $usr_tbl VALUES('1', 'default', '$dfpass')";
    mysql_query($query, $id);
    
    $query = "INSERT INTO $gal_tbl VALUES('1','1','example.jpg','This is the example image ')";    
    mysql_query($query, $id);
    
    mysql_close($id);
    echo "Installation gallery finished !<br />";
}
else
{
    echo "<form method='post' action='install.php'>
    ";
    echo "<input type='hidden' name='install' />
    ";
    echo "DB server hostname: <input type='text' name='db_host' />
    ";
    echo "<br />
    ";
    echo "DB server username: <input type='text' name='db_usr' />"
    ;
    echo "<br />
    ";
    echo "DB server password: <input type='password' name='db_pass' />"
    ;
    echo "<br /><br /><br />
    ";
    echo "Album DB name: <input type='text' name='db_name' />"
    ;
    echo "<br />
    ";
    echo "Users table name: <input type='text' name='usr_tbl' />"
    ;
    echo "<br />
    ";
    echo "Image table name: <input type='text' name='gal_tbl' />"
    ;
    echo "<br />
    ";
    echo "Image comments table name: <input type='text' name='discuz_tbl' />"
    ;
    echo "<br /><br /><br />
    ";    
    
    echo "Album height: <input type='text' name='alb_h' />"
    ;
    echo "<br />
    ";
    
    echo "Album width: <input type='text' name='alb_w' />"
    ;
    echo "<br />
    ";
    
    echo "<input type='submit' value='install' />
    ";
    echo "</form>";
}
?>
</body>
</html>